<template>
  <!--开关，单选 -->
  <el-switch
    v-if="meta.controlType === EControlType.switch"
    v-model="value"
    v-bind="$attrs"
    :id="'c' + meta.columnId"
    :name="'c' + meta.columnId"
    :clearable="clearable"
  >
  </el-switch>
  <!--勾选框-->
  <el-checkbox
    v-else
    v-model="value"
    v-bind="$attrs"
    :id="'c' + meta.columnId"
    :name="'c' + meta.columnId"
    :clearable="clearable"
  >
    {{title}}
  </el-checkbox>
  <circle-close-filled v-if="clearable"
    style="width: 1.2em; height: 1.2em; margin-top: 0px; margin-left: 5px;cursor: pointer;"
    @click.stop="myclear" 
  />
</template>

<script setup lang="ts" generic="T extends object">
  import { nextTick } from 'vue'
  import { CircleCloseFilled } from '@element-plus/icons-vue'
  // 引入组件需要的属性 引入表单子控件的管理类
  import { itemController, EControlType } from '../map'
  import type { IFormChildProps } from '../map'
  import type { Ref } from 'vue'
  
  defineOptions({
    name: 'nf-el-from-item-checkbox',
    inheritAttrs: false,
  })

  // 定义 props
  const props = withDefaults(defineProps<IFormChildProps<T> & {
    title?: string 
  }>(), {
    meta: () => {
      return {
        columnId: 1,
        colName: '',
        controlType: EControlType.switch
      }
    },
    model: () => {
      return {} as T
    },
    clearable: true,
    title: ''
  })

  const { value } = itemController(props)

  const myclear = () => {
    if (value) {
      (value as Ref).value = false
      nextTick(() => {
        setTimeout(() => {
          (value as Ref).value = undefined
        }, 300)
      })
    }
  }
 
</script>
